﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Swd05.SH.Interfaces;
using NUnit.Framework;

namespace Swd05.SH
{
	internal class FactoryTester
	{
		private IScriptFactory factory = null;
		internal FactoryTester( IScriptFactory factory )
		{
			this.factory = factory;
		}
		#region Properties
		IScriptFactory Factory
		{
			get
			{
				return this.factory;
			}
		}
		#endregion
		#region Methods
		internal void FindNonExistingScript()
		{
			Assert.IsFalse( this.Factory.Exists( "not_there" ) , "Script with Id 'not_here' should not be there!" );
		}
		internal void FindExistingScriptById()
		{
			this.Factory.Commit("i_am_here", "script");
			Assert.IsTrue(this.Factory.FindById("i_am_here").Length > 0, "Script with Id 'i_am_here' must be here!");
			this.Factory.Commit("i_am_here", ""); //Remove it
			Assert.IsFalse(this.Factory.Exists("i_am_here"), "Script with Id 'i_am_here' should not be there!");

		}
		internal void RetrieveScriptContent()
		{
			this.Factory.Commit("i_am_here", "script");
			Assert.IsTrue(this.Factory.FindById("i_am_here") == "script", "Script mit Id 'i_am_here' muss gefunden werden!");
			this.Factory.Commit("i_am_here", ""); //Remove it
			Assert.IsFalse(this.Factory.Exists("i_am_here"), "Script mit Id 'i_am_here' darf nicht vorhanden sein!");
		}
		internal void UpdateScriptContent()
		{
			this.Factory.Commit("i_am_here", "script");
			Assert.IsTrue(this.Factory.FindById("i_am_here") == "script", "Script mit Id 'i_am_here' muss gefunden werden!");
			this.Factory.Commit("i_am_here", "script_1");
			Assert.IsTrue(this.Factory.FindById("i_am_here") == "script_1", "Script mit Id 'i_am_here' hat einen ungültigen Wert.");
			this.Factory.Commit("i_am_here", ""); //Remove it
			Assert.IsFalse(this.Factory.Exists("i_am_here"), "Script mit Id 'i_am_here' darf nicht vorhanden sein!");
		}
		internal void RemoveScriptContent()
		{
			this.Factory.Commit("i_am_here", "script");
			Assert.IsTrue(this.Factory.FindById("i_am_here") == "script", "Script mit Id 'i_am_here' muss gefunden werden!");
			this.Factory.Commit("i_am_here", "");
			Assert.IsFalse(this.Factory.Exists("i_am_here"), "Script mit Id 'i_am_here' darf nicht vorhanden sein!");
		}
		internal void Load()
		{
			DataTable dt = null;
			for( int i=0; i < 100; i++ )
			{
				this.Factory.Commit( string.Format( "Script_{0}", i.ToString() ), i.ToString() );
			}
			
			dt = this.Factory.Load();
			
			Assert.IsTrue( dt.Rows.Count == 100, "Anzahl der Scripte muss '100' sein!" );

			for (int i = 0; i < 100; i++)
			{
				this.Factory.Commit(string.Format("Script_{0}", i.ToString()), String.Empty );
			}
			dt = this.Factory.Load();
			Assert.IsTrue(dt.Rows.Count == 0, "Anzahl der Scripte muss '0' sein!");
		}
		#endregion
		
	}
}
